package com.cff.action.suggest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cff.domain.User;
import com.cff.domain.UserTask;
import com.cff.domain.UserType;
import com.cff.service.suggest.SuggestService;
import com.cff.service.user.UserService;
import com.cff.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/suggestRestful")
public class SuggestRestful {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;
  
	@Autowired
	private RepositoryService repositoryService;
	
	@Autowired
	private HistoryService historyService;
	
	@Autowired
	private SuggestService suggestService;
	
	@Autowired
	private UserService userService;
	
	
	
	@RequestMapping(value="/getApplylist", method = RequestMethod.GET)
	public ModelAndView applyList() throws Exception{
		String userId = "15607110725";
		List<UserTask> tasks = suggestService.getUserTask(userId);
		for(int i =0 ;i < tasks.size();i++){
			UserTask tmp = tasks.get(i);
			tmp.setTasktype(convertToType(tmp.getTasktype()));
			tmp.setCurviewer(tmp.getCurviewer() == null ? "" : tmp.getCurviewer());
		}
		System.out.println(tasks);
		return new ModelAndView("xmlView",
				BindingResult.MODEL_KEY_PREFIX + "getApplylist",
				tasks);
	}
	
	@RequestMapping(value="/getApplyList/{id}", method = RequestMethod.GET)
	public ModelAndView getApplyList(@PathVariable("id") String id) throws Exception{
		String userId = id;
		List<UserTask> tasks = suggestService.getUserTask(userId);
		for(int i =0 ;i < tasks.size();i++){
			UserTask tmp = tasks.get(i);
			tmp.setTasktype(convertToType(tmp.getTasktype()));
			tmp.setCurviewer(tmp.getCurviewer() == null ? "" : tmp.getCurviewer());
		}
		System.out.println(tasks);
		return new ModelAndView("xmlView",
				BindingResult.MODEL_KEY_PREFIX + "getApplylist",
				tasks);
	}
	
	

	public String convertType(String type){
		if("基础服务".equals(type)){
			return "01";
		}
		if("技术支持".equals(type)){
			return "02";
		}
		if("产品申请".equals(type)){
			return "03";
		}
		if("会议申请".equals(type)){
			return "04";
		}
		else
			return "05";
		
	}
	
	public String convertToType(String name){
		if("01".equals(name)){
			return "基础服务";
		}
		if("02".equals(name)){
			return "技术支持";
		}
		if("03".equals(name)){
			return "产品申请";
		}
		if("04".equals(name)){
			return "会议申请";
		}
		else
			return "其他";
		
	}
}
